package junit.framework;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Enumeration;
import java.util.Vector;

import junit.panda.TestCasesManager;
import junit.runner.BaseTestRunner;

/**
 * A <code>TestSuite</code> is a <code>Composite</code> of Tests.
 * It runs a collection of test cases. Here is an example using
 * the dynamic test definition.
 * <pre>
 * TestSuite suite= new TestSuite();
 * suite.addTest(new MathTest("testAdd"));
 * suite.addTest(new MathTest("testDivideByZero"));
 * </pre>
 * Alternatively, a TestSuite can extract the tests to be run automatically.
 * To do so you pass the class of your TestCase class to the
 * TestSuite constructor.
 * <pre>
 * TestSuite suite= new TestSuite(MathTest.class);
 * </pre>
 * This constructor creates a suite with all the methods
 * starting with "test" that take no arguments.
 * <p>
 * A final option is to do the same for a large array of test classes.
 * <pre>
 * Class[] testClasses = { MathTest.class, AnotherTest.class }
 * TestSuite suite= new TestSuite(testClasses);
 * </pre>
 *
 * @see Test
 */
public class TestSuite implements Test {
	// Added by Pei Xiaoxing
	static public TestCase createTest(String testName, String name) {
		TestCase testCase = TestCasesManager.get(testName);
		testCase.setName(name);
		return testCase;
	}

	/**
	 * Returns a test which will fail and log a warning message.
	 */
	public static Test warning(final String message) {
		return new TestCase("warning") {
			protected void runTest() {
				fail(message);
			}
		};
	}

	private String fName;

	private Vector fTests= new Vector(10);

	/**
	 * Constructs a TestSuite from the given class. Adds all the methods
	 * starting with "test" as test cases to the suite.
	 * Parts of this method was written at 2337 meters in the Hueffihuette,
	 * Kanton Uri
	 */
	// Added by Pei Xiaoxing
	public TestSuite(String testName) {
		fName = testName;
	}

	/**
	 * Adds a test to the suite.
	 */
	public void addTest(Test test) {
		fTests.addElement(test);
	}

	/**
	 * Adds the tests from the given class to the suite
	 */
	// Added by Pei Xiaoxing
	public void addTestSuite(TestCase testCase) {
		addTest(getTest(testCase.name()));
	}

	// Added by Pei Xiaoxing
	public static Test getTest(String name) {
		TestCase testCase = TestCasesManager.getProtptype(name);
		Class<?> clz = testCase.getClass();
		try {
			Method suiteMethod = clz.getMethod("suite", new Class[0]);

			TestSuite suite = (TestSuite) suiteMethod.invoke(testCase);
			suite.setName(name);

			return suite;
	 	} catch(Exception e) {
	 		TestSuite testSuite = new TestSuite(name);
			Vector names= new Vector();
			for (Method method: testCase.getClass().getDeclaredMethods()) {
				testSuite.addTestMethod(method, names, name);
			}
			if (testSuite.fTests.size() == 0)
				testSuite.addTest(warning("No tests found in "+ name));
			return testSuite;
		}
	}

	/**
	 * Counts the number of test cases that will be run by this test.
	 */
	public int countTestCases() {
		int count= 0;
		for (Enumeration e= tests(); e.hasMoreElements(); ) {
			Test test= (Test)e.nextElement();
			count= count + test.countTestCases();
		}
		return count;
	}

	/**
	 * Returns the name of the suite. Not all
	 * test suites have a name and this method
	 * can return null.
	 */
	public String getName() {
		return fName;
	}
	 
	/**
	 * Runs the tests and collects their result in a TestResult.
	 */
	public void run(TestResult result) {
		for (Enumeration e= tests(); e.hasMoreElements(); ) {
	  		if (result.shouldStop() )
	  			break;
			Test test= (Test)e.nextElement();
			runTest(test, result);
		}
	}

	public void runTest(Test test, TestResult result) {
		test.run(result);
	}
	 
	/**
	 * Sets the name of the suite.
	 * @param name The name to set
	 */
	public void setName(String name) {
		fName= name;
	}

	/**
	 * Returns the test at the given index
	 */
	public Test testAt(int index) {
		return (Test)fTests.elementAt(index);
	}
	
	/**
	 * Returns the number of tests in this suite
	 */
	public int testCount() {
		return fTests.size();
	}
	
	/**
	 * Returns the tests as an enumeration
	 */
	public Enumeration tests() {
		return fTests.elements();
	}
	
	/**
	 */
	public String toString() {
		if (getName() != null)
			return getName();
		return super.toString();
	 }

	private void addTestMethod(Method m, Vector names, String testName) {
		String name= m.getName();
		if (names.contains(name))
			return;
		if (! isPublicTestMethod(m)) {
			if (isTestMethod(m))
				addTest(warning("Test method isn't public: "+m.getName()));
			return;
		}
		names.addElement(name);
		addTest(createTest(testName, name));
	}

	private boolean isPublicTestMethod(Method m) {
		return isTestMethod(m) && Modifier.isPublic(m.getModifiers());
	 }
	 
	private boolean isTestMethod(Method m) {
		String name= m.getName();
		Class[] parameters= m.getParameterTypes();
		// Modified by Pei Xiaoxing
		//Class returnType= m.getReturnType();
		//return parameters.length == 0 && name.startsWith("test") && returnType.equals(Void.TYPE);
		return parameters.length == 0 && name.startsWith("test");
	 }
}